home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / system-config-printer / GroupsPane.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-10-28  |  15KB  |  354 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import gtk
  5. import gobject
  6. from gettext import gettext as _
  7. from GroupsPaneModel import *
  8. from XmlHelper import xml_helper
  9.  
  10. class GroupsPane(gtk.ScrolledWindow):
  11.     __gsignals__ = {
  12.         'item-activated': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, [
  13.             GroupsPaneItem]),
  14.         'items-changed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, []) }
  15.     
  16.     def __init__(self):
  17.         super(GroupsPane, self).__init__()
  18.         self.tree_view = None
  19.         self.store = None
  20.         self.groups_menu = None
  21.         self.ui_manager = None
  22.         self.currently_selected_queues = []
  23.         self.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
  24.         self.tree_view = gtk.TreeView()
  25.         self.tree_view.set_headers_visible(False)
  26.         column = gtk.TreeViewColumn()
  27.         cell = gtk.CellRendererPixbuf()
  28.         column.pack_start(cell, False)
  29.         column.set_cell_data_func(cell, self.icon_cell_data_func)
  30.         cell = gtk.CellRendererText()
  31.         column.pack_start(cell, True)
  32.         column.set_cell_data_func(cell, self.label_cell_data_func)
  33.         cell.connect('edited', self.on_group_name_edited)
  34.         cell.connect('editing-canceled', self.on_group_name_editing_canceled)
  35.         self.tree_view.set_row_separator_func(self.row_separator_func)
  36.         self.tree_view.append_column(column)
  37.         self.store = GroupsPaneModel()
  38.         self.tree_view.set_model(self.store)
  39.         self.add(self.tree_view)
  40.         self.show_all()
  41.         self.tree_view.connect('key-press-event', self.on_key_press_event)
  42.         self.tree_view.connect('button-press-event', self.on_button_press_event)
  43.         self.tree_view.connect('button-press-event', self.on_single_click_activate)
  44.         self.tree_view.connect('row-activated', self.on_row_activated)
  45.         self.tree_view.connect('popup-menu', self.on_popup_menu)
  46.         self.tree_view.enable_model_drag_dest([
  47.             ('queue', 0, 0)], gtk.gdk.ACTION_COPY)
  48.         self.tree_view.connect('drag-data-received', self.on_drag_data_received)
  49.         self.tree_view.connect('drag-drop', self.on_drag_drop)
  50.         self.tree_view.connect('drag-motion', self.on_drag_motion)
  51.         action_group = gtk.ActionGroup('GroupsPaneActionGroup')
  52.         action_group.add_actions([
  53.             ('new-group', gtk.STOCK_NEW, _('_New Group'), '<Ctrl>g', None, self.on_new_group_activate),
  54.             ('new-group-from-selection', None, _('_New Group from Selection'), '<Ctrl><Shift>g', None, self.on_new_group_from_selection_activate),
  55.             ('rename-group', None, _('_Rename'), None, None, self.on_rename_group_activate),
  56.             ('delete-group', gtk.STOCK_DELETE, None, None, None, self.on_delete_group_activate)])
  57.         action_group.get_action('new-group-from-selection').set_sensitive(False)
  58.         action_group.get_action('rename-group').set_sensitive(False)
  59.         action_group.get_action('delete-group').set_sensitive(False)
  60.         self.ui_manager = gtk.UIManager()
  61.         self.ui_manager.insert_action_group(action_group, -1)
  62.         self.ui_manager.add_ui_from_string('\n<ui>\n <accelerator action="new-group"/>\n <accelerator action="new-group-from-selection"/>\n <accelerator action="rename-group"/>\n <accelerator action="delete-group"/>\n</ui>\n')
  63.         self.ui_manager.ensure_update()
  64.         self.groups_menu = gtk.Menu()
  65.         for action_name in [
  66.             'new-group',
  67.             'new-group-from-selection',
  68.             None,
  69.             'rename-group',
  70.             'delete-group']:
  71.             if not action_name:
  72.                 item = gtk.SeparatorMenuItem()
  73.             else:
  74.                 action = action_group.get_action(action_name)
  75.                 item = action.create_menu_item()
  76.             item.show()
  77.             self.groups_menu.append(item)
  78.         
  79.         selection = self.tree_view.get_selection()
  80.         selection.connect('changed', self.on_selection_changed)
  81.         selection.set_mode(gtk.SELECTION_BROWSE)
  82.         selection.select_iter(self.store.append(AllPrintersItem()))
  83.         self.store.append(SeparatorItem())
  84.         for group_name, group_node in xml_helper.get_static_groups():
  85.             self.store.append(StaticGroupItem(group_name, group_node))
  86.         
  87.         for group_name, group_node in xml_helper.get_search_groups():
  88.             self.store.append(SavedSearchGroupItem(group_name, xml_node = group_node))
  89.         
  90.  
  91.     
  92.     def icon_cell_data_func(self, column, cell, model, iter):
  93.         icon = model.get(iter).icon
  94.         cell.set_properties(pixbuf = icon)
  95.  
  96.     
  97.     def label_cell_data_func(self, column, cell, model, iter):
  98.         label = model.get(iter).name
  99.         cell.set_properties(text = label)
  100.  
  101.     
  102.     def on_group_name_edited(self, cell, path, new_text):
  103.         cell.set_properties(editable = False)
  104.         group_item = self.get_selected_item()
  105.         if group_item.name == new_text:
  106.             return None
  107.         if self.store.lookup_by_name(new_text):
  108.             dialog = gtk.MessageDialog(self.get_toplevel(), gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, _('The item could not be renamed.'))
  109.             dialog.format_secondary_text(_('The name "%s" is already in use. Please use a different name.') % new_text)
  110.             dialog.connect('response', (lambda dialog, x: dialog.destroy()))
  111.             dialog.show()
  112.             return None
  113.         group_item.rename(new_text)
  114.         self.emit('items-changed')
  115.  
  116.     
  117.     def on_group_name_editing_canceled(self, cell):
  118.         cell.set_properties(editable = False)
  119.  
  120.     
  121.     def on_key_press_event(self, tree_view, event):
  122.         modifiers = gtk.accelerator_get_default_mod_mask()
  123.         if (event.keyval == gtk.keysyms.BackSpace and event.keyval == gtk.keysyms.Delete or event.keyval == gtk.keysyms.KP_Delete) and event.state & modifiers == 0:
  124.             self.delete_selected_group()
  125.             return True
  126.         if event.keyval == gtk.keysyms.F2 and event.state & modifiers == 0:
  127.             self.rename_selected_group()
  128.             return True
  129.         return False
  130.  
  131.     
  132.     def on_single_click_activate(self, tree_view, event):
  133.         if event.button == 1:
  134.             t = self.tree_view.get_path_at_pos(int(event.x), int(event.y))
  135.             if t != None:
  136.                 self.tree_view.row_activated(t[0], t[1])
  137.             
  138.         
  139.         return False
  140.  
  141.     
  142.     def on_row_activated(self, tree_view, path, column):
  143.         tree_view.get_selection().select_path(path)
  144.         item = self.store.get(path)
  145.         self.emit('item-activated', item)
  146.  
  147.     
  148.     def on_selection_changed(self, selection):
  149.         (model, titer) = selection.get_selected()
  150.         group_item = model.get(titer)
  151.         sensitivity = isinstance(group_item, MutableItem)
  152.         self.ui_manager.get_action('/rename-group').set_sensitive(sensitivity)
  153.         self.ui_manager.get_action('/delete-group').set_sensitive(sensitivity)
  154.  
  155.     
  156.     def row_separator_func(self, model, iter):
  157.         return model.get(iter).separator
  158.  
  159.     
  160.     def do_popup_menu(self, event):
  161.         button = 0
  162.         activate_time = 0x0L
  163.         if event:
  164.             activate_time = event.time
  165.             if event.type != gtk.gdk.BUTTON_RELEASE:
  166.                 button = event.button
  167.             
  168.         
  169.         self.groups_menu.popup(None, None, None, button, activate_time)
  170.  
  171.     
  172.     def on_button_press_event(self, tree_view, event):
  173.         if event.button == 3:
  174.             selection = tree_view.get_selection()
  175.             (model, selected_paths) = selection.get_selected_rows()
  176.             click_info = tree_view.get_path_at_pos(int(event.x), int(event.y))
  177.             if click_info and click_info[0] not in selected_paths:
  178.                 selection.select_path(click_info[0])
  179.             
  180.             self.do_popup_menu(event)
  181.         
  182.         return False
  183.  
  184.     
  185.     def on_popup_menu(self, tree_view):
  186.         self.do_popup_menu(None)
  187.         return True
  188.  
  189.     
  190.     def on_rename_group_activate(self, UNUSED):
  191.         self.rename_selected_group()
  192.  
  193.     
  194.     def on_delete_group_activate(self, UNUSED):
  195.         self.delete_selected_group()
  196.  
  197.     
  198.     def rename_selected_group(self):
  199.         (model, titer) = self.tree_view.get_selection().get_selected()
  200.         group_item = model.get(titer)
  201.         if not isinstance(group_item, MutableItem):
  202.             return None
  203.         column = self.tree_view.get_column(0)
  204.         cell = None
  205.         for cr in column.get_cell_renderers():
  206.             if isinstance(cr, gtk.CellRendererText):
  207.                 cell = cr
  208.                 break
  209.                 continue
  210.             isinstance(group_item, MutableItem)
  211.         
  212.         cell.set_properties(editable = True)
  213.         self.tree_view.set_cursor_on_cell(model.get_path(titer), column, cell, True)
  214.  
  215.     
  216.     def delete_selected_group(self):
  217.         (model, titer) = self.tree_view.get_selection().get_selected()
  218.         group_item = model.get(titer)
  219.         if not isinstance(group_item, MutableItem):
  220.             return None
  221.         dialog = gtk.MessageDialog(self.get_toplevel(), gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_MODAL, gtk.MESSAGE_WARNING, gtk.BUTTONS_NONE, _('Are you sure you want to permanently delete "%s"?') % group_item.name)
  222.         dialog.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_DELETE, gtk.RESPONSE_ACCEPT)
  223.         dialog.set_default_response(gtk.RESPONSE_REJECT)
  224.         dialog.format_secondary_text(_("This will not delete any printer queues from your computer. To delete queues completely, you must delete them from the 'All Printers' group."))
  225.         dialog.connect('response', self.on_delete_selected_group_response, group_item, titer)
  226.         dialog.show()
  227.  
  228.     
  229.     def on_delete_selected_group_response(self, dialog, response, group_item, titer):
  230.         dialog.destroy()
  231.         if response == gtk.RESPONSE_ACCEPT:
  232.             self.tree_view.row_activated(self.store.get_path(self.store.get_iter_first()), self.tree_view.get_column(0))
  233.             self.store.remove(titer)
  234.             group_item.delete()
  235.             self.emit('items-changed')
  236.         
  237.  
  238.     
  239.     def get_selected_item(self):
  240.         (model, titer) = self.tree_view.get_selection().get_selected()
  241.         return model.get(titer)
  242.  
  243.     
  244.     def generate_new_group_name(self):
  245.         name = _('New Group')
  246.         if not self.store.lookup_by_name(name):
  247.             return name
  248.         i = 1
  249.         while True:
  250.             new_name = name + ' %d' % i
  251.             if not self.store.lookup_by_name(new_name):
  252.                 return new_name
  253.             i += 1
  254.             continue
  255.             self.store.lookup_by_name(new_name)
  256.  
  257.     
  258.     def _create_new_group_common(self, item):
  259.         titer = self.store.append_by_type(item)
  260.         self.emit('items-changed')
  261.         self.tree_view.row_activated(self.store.get_path(titer), self.tree_view.get_column(0))
  262.         self.rename_selected_group()
  263.  
  264.     
  265.     def create_new_search_group(self, criterion, group_name = None):
  266.         if not group_name or group_name:
  267.             pass
  268.         item = SavedSearchGroupItem(self.generate_new_group_name(), criteria = [
  269.             criterion])
  270.         self._create_new_group_common(item)
  271.  
  272.     
  273.     def create_new_group(self, printer_queues, group_name = None):
  274.         if not group_name or group_name:
  275.             pass
  276.         item = StaticGroupItem(self.generate_new_group_name())
  277.         item.add_queues(printer_queues)
  278.         self._create_new_group_common(item)
  279.  
  280.     
  281.     def on_new_group_activate(self, UNUSED):
  282.         self.create_new_group([])
  283.  
  284.     
  285.     def on_new_group_from_selection_activate(self, UNUSED):
  286.         self.create_new_group(self.currently_selected_queues)
  287.  
  288.     
  289.     def is_drop_target(self, tree_view, x, y):
  290.         
  291.         try:
  292.             (path, position) = self.tree_view.get_dest_row_at_pos(x, y)
  293.             group_item = self.store.get(path)
  294.         except TypeError:
  295.             return False
  296.  
  297.         if not isinstance(group_item, StaticGroupItem):
  298.             return False
  299.         return True
  300.  
  301.     
  302.     def on_drag_data_received(self, tree_view, context, x, y, selection_data, info, timestamp):
  303.         if not (selection_data.data) or info != 0:
  304.             context.finish(False, False, timestamp)
  305.             return None
  306.         if not self.is_drop_target(tree_view, x, y):
  307.             context.finish(False, False, timestamp)
  308.             return None
  309.         (path, position) = self.tree_view.get_dest_row_at_pos(x, y)
  310.         group_item = self.store.get(path)
  311.         group_item.add_queues(selection_data.data.splitlines())
  312.         context.finish(True, False, timestamp)
  313.  
  314.     
  315.     def on_drag_drop(self, tree_view, context, x, y, timestamp):
  316.         if not self.is_drop_target(tree_view, x, y):
  317.             return False
  318.         target_list = tree_view.drag_dest_get_target_list()
  319.         target = tree_view.drag_dest_find_target(context, target_list)
  320.         tree_view.drag_get_data(context, target, timestamp)
  321.         return True
  322.  
  323.     
  324.     def on_drag_motion(self, tree_view, context, x, y, timestamp):
  325.         if not self.is_drop_target(tree_view, x, y):
  326.             return False
  327.         (path, position) = tree_view.get_dest_row_at_pos(x, y)
  328.         tree_view.set_drag_dest_row(path, position)
  329.         context.drag_status(gtk.gdk.ACTION_COPY, timestamp)
  330.         return True
  331.  
  332.     
  333.     def get_static_groups(self):
  334.         static_groups = []
  335.         for row in self.store:
  336.             if isinstance(row[0], StaticGroupItem):
  337.                 static_groups.append(row[0])
  338.                 continue
  339.         
  340.         return static_groups
  341.  
  342.     
  343.     def n_groups(self):
  344.         n = 0
  345.         for row in self.store:
  346.             if isinstance(row[0], StaticGroupItem) or isinstance(row[0], SavedSearchGroupItem):
  347.                 n += 1
  348.                 continue
  349.         
  350.         return n
  351.  
  352.  
  353. gobject.type_register(GroupsPane)
  354.